extends Node2D
@export var player_spawn:Marker2D
@onready var many_eye: Node2D = $"../Effect/ManyEye"
@onready var player_death_effect: BlackHole = %PlayerDeathEffect
@onready var eye_trans: Control = %EyeTrans
func save_data()->void:
	var dic:Dictionary
	dic["triggered"]=triggered
	var file:=FileAccess.open(Global.save_dictionary+"begin_save.json",FileAccess.WRITE)
	if not file:return
	file.store_string(JSON.stringify(dic))
	file.close()
func load_if_data()->void:
	if Global.enter_said_should_restart:return
	await $"../..".ready
	if $"../.."._should_start_from_begin:return
	if FileAccess.file_exists(Global.save_path+"begin_save.json"):
		var file:=FileAccess.open(Global.save_path+"begin_save.json",FileAccess.READ)
		var dic:Dictionary=JSON.parse_string(file.get_as_text())
		file.close()
		print(dic)
		if dic.has("triggered"):
			for tri in dic["triggered"]:
				if tri is String:triggered.push_back(tri)
func _ready() -> void:
	if not player_spawn:player_spawn=$"../../Marks/PlayerSpawner"
	Global.save_game_level.connect(save_data)
	player_death_effect.visible=false
	load_if_data()
	Global.player_should_death.connect(player_death)
	for child in $Mans.get_children():
		var area=child.get_child(0)
		if area is Area2D:
			area.area_entered.connect(_on_area_entered.bind("m"+child.name.substr(1)))
	
var _death_twn:Tween
func player_death()->void:
	if _death_twn and _death_twn.is_running():
		return
	player_death_effect.material.set_shader_parameter("strength",0.0)
	player_death_effect.visible=true
	player_death_effect.global_position=Global.player_position
	_death_twn=create_tween()
	_death_twn.tween_property(player_death_effect.material,"shader_parameter/strength",100.0,0.5)
	_death_twn.tween_property(eye_trans,"blur",5.0,0.5)
	_death_twn.tween_callback(func():
		Global.player_movment_mode.emit("walk")
		%Player.position=player_spawn.global_position
		player_death_effect.visible=false
		)
	_death_twn.tween_property(eye_trans,"blur",0.0,0.5)
var _can_close_eye:bool=false
var _should_colse_eye:bool=false
func _process(delta: float) -> void:
	if _should_colse_eye and _can_close_eye:
		%EyeTrans.width=move_toward(%EyeTrans.width,0.2,delta)
		%EyeTrans.blur=move_toward(%EyeTrans.blur,3,delta*10)
	else:
		%EyeTrans.width=move_toward(%EyeTrans.width,0.5,delta)
		%EyeTrans.blur=move_toward(%EyeTrans.blur,0,delta*5)
#===============================================================================
func _input(event: InputEvent) -> void:
	if event.is_action_released("interaction"):
		if current_tag!="":Global.takeplace_dialogue.emit(current_tag)
	elif event is InputEventMouseButton and event.pressed:
		var dir=event.position-Global.player_screen_pos
		Global.player_suspend_rush.emit(dir,4000)
	elif event.is_action_pressed("close_eye") and $"../..".loadded:
		$ColseEyeTimer.start()
		_should_colse_eye=true
	elif event.is_action_released("close_eye") and $"../..".loadded:
		$ColseEyeTimer.stop()
		_should_colse_eye=false
var current_tag:String = "":
	set(v):
		current_tag=v
		if current_tag=="":
			Global.player_fable.emit(false)
		else:
			Global.player_fable.emit(true)
var triggered:Array[String]
func _on_area_entered(area: Area2D,tag:String) -> void:
	if tag=="":return
	if area.is_in_group("player"):
		if triggered.has(tag):
			if current_tag=="":
				current_tag=tag
		else:
			Global.takeplace_dialogue.emit(tag)
			_handle_if_is_special_tag(tag)
func _on_area_exited(area: Area2D,tag:String) -> void:
	if tag=="":return
	if area.is_in_group("player"):
		if triggered.has(tag):
			if current_tag==tag:
				current_tag=""
		else:
			triggered.push_back(tag)
#===============================================================================
func _handle_if_is_special_tag(tag:String):
	if tag.substr(0,3)=="man":
		_on_mans(tag)
		return
	match tag:
		"find_oldman":_on_find_oldman()
		"oldman":_on_oldman()
		"teachboard":_on_teachboard()
		"firsttest":_on_firsttest()
		"oldpro":_on_oldpro()
func _on_mans(tag:String)->void:
	var man;
	man=$Mans.find_child("M"+tag.substr(1))
	if not man:return
	man.find_child("M"+tag.substr(1)).set_deferred("monitoring",false)
	var _twn:=create_tween()
	var col:Color=man.box_material.get_shader_parameter("color")
	col.a=0.0
	_twn.tween_property(man.box_material,"shader_parameter/strength",0.13,3.0)
	_twn.parallel().tween_property(man.box_material,"shader_parameter/color",col,3.0)
	_twn.parallel().tween_property(man,"eye_alpha",0.0,3.0)
	Global.player_movment_mode.emit("tie")
	await Global.dialogue_finished
	Global.player_movment_mode.emit("walk")
func _on_find_oldman()->void:
	$FindOldman.set_deferred("monitoring",false)
	Global.player_movment_mode.emit("tie")
	Global.player_animation_event.emit("pectinoma")
	%Camera.look_at_pos($Oldman.position)
	Global.display_str.emit("那里有个奇怪的人")
	await Global.display_string_finished
	%Camera.follow_player()
	Global.player_movment_mode.emit("walk")
	Global.display_str.emit("过去看看")
func _on_oldman()->void:
	if not triggered.has("oldman"):
		await get_tree().create_timer(1).timeout
		Global.display_str.emit("好像有什么在注视着我")
		await get_tree().create_timer(5).timeout
		Global.player_movment_mode.emit("tie")
		many_eye.position.x=Global.player_position.x
		many_eye.play()
		await many_eye.played
		Global.player_animation_event.emit("surprise")
		await get_tree().create_timer(0.3).timeout
		player_death()
		many_eye.close()
		await many_eye.closed
		var _twn:Tween=create_tween()
		_twn.tween_property($Oldman,"eye_alpha",0.0,3.0)
		_twn.tween_property($Oldman.box_material,"shader_parameter/alpha",0.0,3.0)
		_twn.tween_callback(func():
			$Oldman.queue_free()
			)
func _on_teachboard()->void:
	if not triggered.has("teachboard"):
		Global.player_movment_mode.emit("tie")
		await Global.dialogue_finished
		Global.player_movment_mode.emit("walk")
func _on_firsttest()->void:
	Global.player_movment_mode.emit("suspend")
func _on_oldpro()->void:
	Global.player_movment_mode.emit("tie")
	await Global.dialogue_finished
	Global.player_movment_mode.emit("walk")
